package com.offer;


import com.utils.DataUtils;

import java.util.Comparator;
import java.util.PriorityQueue;

public class Test41 {

    public Test41() {
    }

    private PriorityQueue<Integer> minQueue = new PriorityQueue<>();
    private PriorityQueue<Integer> maxQueue = new PriorityQueue<>(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    });

    public void addNum(int num) {
        if (maxQueue.size() == minQueue.size()) {
            minQueue.offer(num);
            maxQueue.offer(minQueue.poll());
        } else {
            maxQueue.offer(num);
            minQueue.offer(maxQueue.poll());
        }
    }

    public double findMedian() {
        if (maxQueue.size() == 0 && minQueue.size() == 0) {
            return 0;
        }
        if (maxQueue.size() == minQueue.size()) {
            return (maxQueue.peek() + minQueue.peek()) / 2.0;
        } else {
            return maxQueue.peek() * 1.0;
        }
    }

    public static void main(String[] args) {
        Test41 test34 = new Test41();
        test34.addNum(1);
        test34.addNum(2);
        System.out.println(test34.findMedian());
//        test34.addNum(3);
//        System.out.println(test34.findMedian());
    }

}
